<?php

define('LAYERED_PATH', realpath(dirname(__FILE__)));
set_include_path('.'
	. PATH_SEPARATOR . LAYERED_PATH . '/library'
	. PATH_SEPARATOR . LAYERED_PATH . '/models'
	. PATH_SEPARATOR . get_include_path());

require_once 'Zend/Controller/Plugin/Abstract.php';
require_once 'Zend/Registry.php';
require_once 'LayeredContent.php';

class Layered_Plugin extends Zend_Controller_Plugin_Abstract
{
	public $layeredContent;
	
	public function routeStartup ()
	{
		$this->layeredContent = LayeredContent::getInstance();
	}
	
	public function postDispatch ()
	{
		$response = $this->getResponse();
		$exception = $response->getExceptionByType('Zend_Controller_Action_Exception');
		
		if (!$exception) {
			$exception = $response->getExceptionByType('Zend_Controller_Dispatcher_Exception');
		}
		
		if (!$exception) {
			return;
		}
		
		$request = $this->getRequest();
		if ($request->getModuleName() == 'layered')
			return;
		
		$request->setModuleName('layered');
		$request->setControllerName('resources');
		$request->setActionName('view');
		$request->setDispatched(false);
	}
}